yjx.service('pdfView',['$rootScope','ipCookie','$timeout',function($rootScope,ipCookie,$timeout){
    var pdfScale = 1    ; // make pdfScale a global variable
    var pdfmodal = document.getElementById('pdfModal');
    var canvasp = document.getElementById('pdf-container');
    var shownPdf; // another global we'll use for the buttons
    var pdfScrollTop = 0;
    var lastPdfUrl = '';
    var pzC = pdfmodal.closest('.scrollable-content');
    $rootScope.pdfLoading = true;
    $rootScope.isZooming = false;
    $rootScope.hideTab = true;
return{
      showPdfAct: function(url){
        /*进入页面PDF预览初始化内容*/
        ipCookie('pdfScrollTop', 0);//清除上个PDF的滚动条缓存
        $timeout(function () {
            angular.element(pzC).css('height', document.body.offsetHeight);
        })
        PDFJS.workerSrc = 'common/libs/pdf.worker.js';
        PDFJS.getDocument(url).then(function getPdfHelloWorld(pdf) {
            displayPage(pdf);
            shownPdf = pdf;
        });
    },


    /*放大查看*/
    zoomin:function () {
        //if(removeWidth){
        $timeout(function () {
            angular.element(pdfmodal).find('canvas').css('width','auto')
        })
        //}

        if (!$rootScope.isZooming) {
            $rootScope.isZooming = true;
            resetScrollTop(true);
            pdfScale = pdfScale + 0.25;
            displayPage(shownPdf);
        }
    },
    /*缩小查看*/
    zoomout:function () {
        if (!$rootScope.isZooming) {
            $rootScope.isZooming = true;
            if (pdfScale <= 0.25) {
                return;
            }
            resetScrollTop(false);
            pdfScale = pdfScale - 0.25;
            displayPage(shownPdf);
        }

    }

}
/*为当前浏览位置重新计算滚动条*/
    function resetScrollTop(isPlus) {
        var pdfScrollTop0 = ipCookie('pdfScrollTop');
        if (pdfScrollTop0 > 10) {
            if (isPlus) {
                pdfScrollTop = parseInt(pdfScrollTop0 * pdfScale / pdfScale + 0.25)
            } else {
                pdfScrollTop = parseInt(pdfScrollTop0 * pdfScale / pdfScale - 0.25)
            }
        }
    }
    function renderPage(page, isSingle) {
        var scale = pdfScale; // render with global pdfScale variable
        var viewport = page.getViewport(scale);
        var canvas;
        if (isSingle) {
            canvas = canvasp
        } else {
            canvas = document.createElement('canvas');
            canvas.className = 'pagesCanvas';
        }
        var context = canvas.getContext('2d');
        canvas.height = viewport.height;
        canvas.width = viewport.width;
        pdfmodal.appendChild(canvas);
        var renderContext = {
            canvasContext: context,
            viewport: viewport
        };
        $timeout(function () {
            page.render(renderContext);
            $rootScope.pdfLoading = false;
            $rootScope.isZooming = false;
            if (pdfScrollTop) {
                angular.element(pzC).css('scrollTop', pdfScrollTop);//将滚动条设置到当前浏览位置
            }
        })
    }

    function displayPage(pdf) {
        if (pdf.numPages > 1) {
            canvasp.style.display = "none";
            angular.element(document.querySelectorAll('.pagesCanvas')).detach();
            for (var i = 1; i < pdf.numPages; i++) {
                pdf.getPage(i).then(function getPage(page) {
                    renderPage(page, false);
                });
            }
        } else {
            canvasp.style.display = "inline-block";
            pdf.getPage(1).then(function getPage(page) {
                renderPage(page, true);
            });
        }
    }
}])